﻿using System;
using System.Web;
using System.Reflection;
using System.IO;

namespace SocialCash.Web.Facebook
{    
    /// <summary>An implementation of <see cref="IHttpHandler" /> that renders the <c>Headliners</c> receiver page.</summary>
    public class HeadlinersReceiverHandler : IHttpHandler
    {
        private static readonly DateTime LastUpdated = new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTimeUtc;

        #region [ IHttpHandler Members ]

        Boolean IHttpHandler.IsReusable
        {
            get { return true; }
        }

        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetLastModified(HeadlinersReceiverHandler.LastUpdated);

            DateTime clientLastModified;
            if (DateTime.TryParse(context.Request.Headers["If-Modified-Since"], out clientLastModified))
            {
                context.Response.AppendHeader("Date", DateTime.UtcNow.ToString());
                context.Response.StatusCode = 304;
            }
            else
            {
                context.Response.Write(Content.Receiver);
            }
        }

        #endregion
    }
}